package com.spdzen.support.result;

import java.io.Serializable;

/**
 * 通用返回的JSON格式
 * 
 * @author accountwcx@qq.com
 *
 */
public class JsonResult implements Serializable {
	private static final long serialVersionUID = 858222258187852367L;
	
	/**
	 * 执行结果代码，默认是0
	 */
	private String code = BaseResultCode.getSuccessCode();

	/**
	 * 客户端提示消息，如果message有值客户端需要显示。
	 */
	private String message;

	/**
	 * 返回数据
	 */
	private Object data;
	
	public JsonResult() {
	}
	
	public JsonResult(ResultCode resultCode) {
		this.code = resultCode.getCode();
		this.message = resultCode.getMessage();
	}
	
	public JsonResult(ResultCode resultCode, Object data) {
		this.code = resultCode.getCode();
		this.message = resultCode.getMessage();
		this.data = data;
	}
	
	public static JsonResult success() {
		return new JsonResult();
	}

	public static JsonResult success(Object data) {
		JsonResult ret = new JsonResult();
		ret.setData(data);
		return ret;
	}

	public static JsonResult error() {
		JsonResult ret = new JsonResult();
		ret.setCode(BaseResultCode.getErrorCode());
		return ret;
	}
	
	public static JsonResult error(String message) {
		JsonResult ret = new JsonResult();
		ret.setCode(BaseResultCode.getErrorCode());
		ret.setMessage(message);
		return ret;
	}
	
	public static JsonResult error(ResultCode resultCode) {
		JsonResult ret = new JsonResult();
		ret.setCode(resultCode.getCode());
		ret.setMessage(resultCode.getMessage());
		return ret;
	}
	
	public static JsonResult error(String code, String message) {
		JsonResult ret = new JsonResult();
		ret.setCode(code);
		ret.setMessage(message);
		return ret;
	}
	
	public static JsonResult errorParameter(String message) {
		JsonResult ret = new JsonResult();
		ret.setCode(BaseResultCode.PARAMETER_ERROR.getCode());
		ret.setMessage(message);
		return ret;
	}
	
	public static JsonResult errorParameter() {
		JsonResult ret = new JsonResult();
		ret.setCode(BaseResultCode.PARAMETER_ERROR.getCode());
		ret.setMessage(BaseResultCode.PARAMETER_ERROR.getMessage());
		return ret;
	}
	
	public static <T> JsonResult instance(SimpleResult<T> simpleResult) {
		JsonResult ret = new JsonResult(simpleResult.getCode(), simpleResult.getData());
		return ret;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
	
}
